/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inventurserver;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author streblow
 */
public class Listener extends Thread {
    
    private final MainFrame m_MainFrameInstance;
    
    public Listener(MainFrame instance) {
        m_MainFrameInstance = instance;
    }
    
    @Override
    public void run() {
        try {
            ServerSocket sSocket;
            sSocket = new ServerSocket(m_MainFrameInstance.m_Port);
            sSocket.setReuseAddress(true);
            while (!m_MainFrameInstance.m_StopRequested) {
                Socket cSocket = sSocket.accept(); //blocking call
                if (!m_MainFrameInstance.m_StopRequested) {// MainFrame did not connect to myself to get control
                    // Communicator-Thread starten
                    (new Thread(new Communicator(m_MainFrameInstance, cSocket))).start();
                }
            }
            sSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
